<?php

namespace App\Admin\Controllers;

use App\Models\Business;
use App\Models\Department;
use App\Models\Task;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class TaskController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '任务计划管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Task);

        $grid->model()->orderBy('start_time', 'desc');

        $grid->column('id','Id');
        $grid->column('department_id','所属部门')->display(function () {
            return $this->department->name;
        });

        $grid->column('business_id','任务类型')->display(function () {
            return $this->business->name;
        });

        $grid->column('plan_number', '计划完成数')->display(function () {
            return $this->plan_number . '('.$this->business->unit.')';
        });

        $grid->column('start_time', '开始时间')->date('Y-m-d');

        $grid->column('end_time', '结束时间')->date('Y-m-d');

        $grid->filter(function ($filter) {
            $filter->column(1/2, function ($filter) {
                $filter->disableIdFilter();
                $filter->equal('business_id', '业务类型')->select('businesses/getBusiness');
                $filter->equal('department_id','所属部门')->select('departments/getDepartment');
                $filter->between('plan_number', '目标值介于');
            });
            $filter->column(1/2, function ($filter){
                $filter->between('start_time', '开始时间')->date();
                $filter->between('end_time', '结束时间')->date();
            });

        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Task::findOrFail($id));



        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Task);

        $form->display('id','ID');
        $form->select('business_id','业务类型')->options(Business::selectOptions());
        $form->select('department_id','所属部门')->options(Department::selectOptions());

        $form->number('plan_number','计划完成数');

        $form->dateRange('start_time', 'end_time', '业务持续时间');
        return $form;
    }
}
